<?php
class modul
    {
    public $modulPath;
    public $baseDir = false;


    /**
    * install a modul
    * 
    */
    function installModul()
        {

        $this->createModulPath();


        // Datenbank Struktur erstellen
        $xmlConfigFilename="{$this->modulPath}/db/db.xml";

        $xmlConfig=new xml_config();
        $xmlConfig->xmlFilename=$xmlConfigFilename;
        $xmlConfig->baseDir=$this->baseDir;
        $xmlConfig->installXmlConfig();


        // Content installieren
        $compileContent=new compile_content();

        $compileContent->baseDir=$this->baseDir;
        $compileContent->contentPath="{$this->modulPath}/content";
        $compileContent->compileContent();


        // Backend Administration erstellen
        /* $backendScaffolding=new scaffolding_administration();
         $backendScaffolding->createAdministration($xmlConfigFilename, true, false);   */


        // Mail kopieren
        $folderMail=new folder();
        $folderMail->baseDir=$this->baseDir;
        //$folderMail->path=config::$basePath . "{$this->modulPath}/mail";
        $folderMail->path="{$this->modulPath}/mail";

        foreach ($folderMail->getFolder() as $item)
            {

            //echo $item['filename'];
            //$filenameTo = config::$projectPath . "/mail/{$item['filename']}";

            //echo $item['filename'];

            $html=file::readTextFile($item['full_filename'], $this->baseDir);

            $filename=re::getHtmlTagValue("filename", $html);
            $subject=re::getHtmlTagValue("subject", $html);
            $mailFrom=re::getHtmlTagValue("mail_from", $html);
            $mailFromText=re::getHtmlTagValue("mail_from_text", $html);
            $mailTo=re::getHtmlTagValue("mail_to", $html);
            $mailCc=re::getHtmlTagValue("mail_cc", $html);
            $mailBcc=re::getHtmlTagValue("mail_bcc", $html);
            $modus=re::getHtmlTagValue("modus", $html);
            $text=re::getHtmlTagValue("body", $html);

            $tableName=re::getHtmlTagValue("table_name", $html);
            
            // falls Mail noch nicht vorhanden ist
            if (db::getCount("_mail", "filename='{$filename}'") == 0)
                {

                $data=array();
                $data['filename']=$filename;
                $data['subject']=$subject;
                $data['mail_from']=$mailFrom;
                $data['mail_from_text']=$mailFromText;
                $data['mail_to']=$mailTo;
                $data['mail_cc']=$mailCc;
                $data['mail_bcc']=$mailBcc;
                $data['modus']=$modus;
                $data['text']=$text;
                $mailId = db::saveData("_mail", $data);
                
                // Mail einer Tabelle zuweisen
                $tableNameId = db::getId("_table", "table_name='{$tableName}'");
                $data = array();
                $data['__table'] = $tableNameId;
                $data['__mail'] = $mailId;
                db::saveData("_table_mail", $data);
                
                
                
                
                
                
                
                
                }


            /*if (!file::fileExists($filenameTo, false))
                {
                file::copyFile($item['full_filename'], $filenameTo, false, false);
                }*/
            }


        /* xml_config::importXmlConfig($xmlConfigFilename, true); 
         backend_scaffolding::backendScaffolding($xmlConfigFilename, true); */

        }


    /**
    * import a modul (copies all files from a modul to a project path)
    * 
    * @param mixed $modulPath
    */
    function importModul()
        {


        //modul::createModulPath($modulPath);


        // Copy Db File
        file::copyFile(config::$basePath . $this->modulPath . "/db/db.xml", config::$projectPath . "/db/blog.xml",
            false, false);

        // Copy Content
        file::copyFolder(config::$basePath . $this->modulPath . "/content/", config::$projectPath . "/content/", false);
        }


    /**
    * create folder for a modul
    * 
    */
    function createModulPath()
        {


        // create modul folders
        file::createFolder($this->modulPath, $this->baseDir);
        file::createFolder("{$this->modulPath}/content", $this->baseDir);
        file::createFolder("{$this->modulPath}/header", $this->baseDir);
        file::createFolder("{$this->modulPath}/com", $this->baseDir);
        file::createFolder("{$this->modulPath}/mail", $this->baseDir);
        file::createFolder("{$this->modulPath}/db", $this->baseDir);
        file::createFolder("{$this->modulPath}/db_data", $this->baseDir);
        }

    function zipModul($zipFilename) {


    //$zipFilename =  "/store/modul/$modul/$modul.zip";
    zip::createZip($this->modulPath, $zipFilename, true); }

    function installZipModul($zipFilename)
        {

        $tmpPath="/tmp/modul_import/";
        file::deleteDirectoryTree($tmpPath, true);
        file::createFolder($tmpPath, true);

        zip::zipExtract($zipFilename, $tmpPath, true);


        // echo $modulFilename;
        //  $modul = new modul();
        $this->baseDir=true;
        //$modul->modulPath = $tmpPath."/Cms/";
        $this->modulPath=$tmpPath;

        $this->installModul();
        }
    }
?>